import { IAccessToken, IErrorResp, IUser } from './type';
import axiosIns from '@/plugins/axios';
import axios from 'axios';

export async function login(params: object) {
  const options = {
    method: 'POST',
    url: `/api/auth/login`,
    data: params,
    headers: {
      'Content-Type': 'application/json'
    }
  };
  return axiosIns.request<IAccessToken>(options);
}

export async function fetchMe() {
  const options = {
    method: 'GET',
    url: `/api/me`,
    headers: {
      'Content-Type': 'application/json'
    }
  };
  return axiosIns.request<IUser | IErrorResp>(options);
}

export async function fetchUser(params) {
  const options = {
    method: 'GET',
    url: `/api/user`,
    params: params,
    headers: {
      'Content-Type': 'application/json'
    }
  };
  return axiosIns.request<Array<IUser>>(options);
}

export async function fetchUserList(includeDeleted: boolean = false) {
  const options = {
    method: 'GET',
    url: `/api/user/list.do`,
    headers: {
      'Content-Type': 'application/json'
    }
  };
  return axiosIns.request(options);
}

export function getUserByStudentId(studentId: string) {
  return axios.get('/api/user/getByStudentId.do', { params: { studentId } });
}

// 获取当前用户信息
export async function getCurrentUser(username: string) {
  const options = {
    method: 'GET',
    url: `/api/user/current.do`,
    params: { username },
    headers: {
      'Content-Type': 'application/json'
    }
  };
  return axiosIns.request(options);
}

// 更新个人资料
export async function updateProfile(profileData: Record<string, any>) {
  const options = {
    method: 'POST',
    url: `/api/user/profile.do`,
    data: profileData,
    headers: {
      'Content-Type': 'application/json'
    }
  };
  return axiosIns.request(options);
}

// 编辑用户
export async function updateUser(userId: number, userData: Record<string, any>) {
  const options = {
    method: 'POST',
    url: `/api/user/update.do`,
    data: {
      id: userId,
      username: userData.username,
      name: userData.name,
      phone: userData.phone,
      gender: userData.gender,
      role: userData.role,
      is_active: userData.is_active,
      extra_info: userData.extra_info
    },
    headers: {
      'Content-Type': 'application/json'
    }
  };
  return axiosIns.request(options);
}

// 添加用户
export async function createUser(userData: Record<string, any>) {
  const options = {
    method: 'POST',
    url: `/api/user/add.do`,
    data: {
      username: userData.username,
      name: userData.name,
      phone: userData.phone,
      gender: userData.gender,
      role: userData.role,
      is_active: userData.is_active,
      extra_info: userData.extra_info,
      password: userData.password || '123456' // 默认密码
    },
    headers: {
      'Content-Type': 'application/json'
    }
  };
  return axiosIns.request(options);
}

// 删除用户
export async function deleteUser(userId: number) {
  const options = {
    method: 'POST',
    url: `/api/user/delete.do`,
    data: {
      id: userId
    },
    headers: {
      'Content-Type': 'application/json'
    }
  };
  return axiosIns.request(options);
}
